package com.all.play.myslidingmenu;

import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        //将actionBar替换成toolbar
        setSupportActionBar(toolbar);

        initView();


        initData();
    }

    private void initData() {

    }

    private void initView() {
        drawerLayout = (DrawerLayout) findViewById(R.id.drawlayout);
        //toolbar 添加返回键
        ActionBarDrawerToggle drawerToggle=
                new ActionBarDrawerToggle(this,drawerLayout,toolbar,
                        R.string.draw_open,R.string.draw_close);
        //drawerToggle与toolbar状态同步
        drawerToggle.syncState();
        //三道杠  侧滑按钮  设置监听
        //说明drawerToogle实现了DrawerListener
        //drawerToggle与toolbar,三道杠的旋转监听设置在这里才能同步
        //drawerLayout.addDrawerListener(drawerToggle);

        //在右侧添加侧滑菜单,在此将之前布局文件做备份
        //定义自己滑动回调,仿QQ抽屉侧滑
        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {

            //
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                //slideOffset,滑动过程中不断变化,0~1
                //获取内容部分
                View content = drawerLayout.getChildAt(0);
                //获得抽屉部分
                View menu=drawerView;

                //设置抽屉布局特效
                float Scale=1-slideOffset;
                float leftScale= (float) (1-0.3*Scale);
                float rightScale= (float) (0.7f+0.3*Scale);
                menu.setScaleX(leftScale);//1~0.7
                menu.setScaleY(leftScale);//1~0.7

                //设置内容布局特效
                content.setScaleX(rightScale);//0.7~1
                content.setScaleY(rightScale);
                //设置平移
                content.setTranslationX(menu.getMeasuredWidth()*(1-Scale));


            }

            @Override
            public void onDrawerOpened(View drawerView) {

            }

            @Override
            public void onDrawerClosed(View drawerView) {

            }
            //状态改变
            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });

    }
}
